package com.penglecode.netty3.example3;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

public class ApplicationServer {

	private static final ChannelFactory channelFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
	
	private static final ServerBootstrap serverBootstrap = new ServerBootstrap(channelFactory);
	
	private int port;
	
	static {
		serverBootstrap.setOption("child.tcpNoDelay", true);
		serverBootstrap.setOption("child.keepAlive", true);
		serverBootstrap.setPipelineFactory(new ServerChannelPipelineFactory());
	}

	public ApplicationServer(int port) {
		super();
		this.port = port;
	}

	public void start() throws Exception {
		serverBootstrap.bind(new InetSocketAddress(InetAddress.getLocalHost(), port));
	}

	public static void main(String[] args) throws Exception {
		new ApplicationServer(9000).start();
	}
	
}
